package com.mdhw.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdhw.common.enums.StatusEnum;
import com.mdhw.common.enums.SysDictEnum;
import com.mdhw.config.R;
import com.mdhw.order.vo.PrizePoolSetVo;
import com.mdhw.system.dao.SysDictDataDao;
import com.mdhw.system.entity.SysDictData;
import com.mdhw.system.service.SysDictDataService;
import com.mdhw.system.service.SysUserService;
import com.mdhw.system.vo.SysUserVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataDao, SysDictData> implements SysDictDataService {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysDictDataDao sysDictDataDao;
    /**
     * 新怎 SysDictData
     * @param sysDictData
     * @return
     */
    @Override
    public R<String> addDictData(SysDictData sysDictData) {
        SysUserVo sysUserVo = this.sysUserService.getUserVoByToken();
        Date nowTime = new Date();
        String dictType = sysDictData.getDictType();
        String dictValue = sysDictData.getDictValue();
        sysDictData.setCreateDate(nowTime);
        sysDictData.setCreateUser(sysUserVo.getLoginName());
        sysDictData.setUpdateDate(nowTime);
        sysDictData.setUpdateUser(sysUserVo.getLoginName());
        sysDictData.setStatus(StatusEnum.OK.getCode().toString());

        boolean isSuccess = this.save(sysDictData);
        if(isSuccess){
            return R.success();
        }else{
            return R.error("操作失败");
        }
    }

    @Override
    public R<String> updateDictData(SysDictData sysDictData){
        String dictType = sysDictData.getDictType();
        String dictValue = sysDictData.getDictValue();
        Long id = sysDictData.getId();
        LambdaQueryWrapper<SysDictData> sddLqw = new LambdaQueryWrapper<>();
        sddLqw.eq(SysDictData::getStatus,StatusEnum.OK.getCode());
        sddLqw.eq(SysDictData::getDictType,dictType);
        sddLqw.eq(SysDictData::getDictValue,dictValue);
        sddLqw.ne(SysDictData::getId,id);
        List<SysDictData> sysDictDataList = this.list(sddLqw);
        if(sysDictDataList != null && sysDictDataList.size() > 0){
            return R.error("dictValue 重复");
        }else{
            if(sysDictData.getDictSort() == null){
                sysDictData.setDictSort(0);
            }
            boolean isSuccess = this.updateById(sysDictData);
            if(isSuccess){
                return R.success("修改成功!");
            }else{
                return R.error("修改失败!");
            }
        }
    }

    @Override
    public R deleteDictData(Long id){
        SysDictData sysDictData = this.getById(id);
        sysDictData.setStatus(StatusEnum.DELETE.getCode());
        boolean isSuccess = this.updateById(sysDictData);
        if(isSuccess){
            return R.success("删除成功!");
        }else{
            return R.error("删除失败!");
        }
    }

    /**
     *
     * @param dictType
     * @return
     */
    @Override
    public List<SysDictData> listByDictType(String dictType) {
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .eq(SysDictData::getDictType, dictType)
                .eq(SysDictData::getStatus, StatusEnum.OK.getCode());

        return this.list(queryWrapper);
    }

    @Override
    public String getLabelByValue(String dictType, String dictValue) {
        if(StringUtils.isBlank(dictValue)){
            return "";
        }
        List<SysDictData> sysDictDataList = this.listByDictType(dictType);
        for(SysDictData sysDictData:sysDictDataList){
            String dictValue1 = sysDictData.getDictValue();
            if(dictValue.equals(dictValue1)){
                return sysDictData.getDictLabel();
            }
        }
        return "";
    }



    @Override
    public Map<String, List<SysDictData>> multiDictDataListByType(String dictType) {
        List<String> split = Arrays.asList(dictType.split(","));
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .in(SysDictData::getDictType, split)
                .eq(SysDictData::getStatus, "1");

        List<SysDictData> sysDictData = sysDictDataDao.selectList(queryWrapper);
        Map<String, List<SysDictData>> collect = sysDictData.stream().collect(Collectors.groupingBy(SysDictData::getDictType));
        return collect;
    }

    /**
     * 奖金池设置
     * @return
     */
    @Override
    public PrizePoolSetVo selectPrizePoolSet() {
        PrizePoolSetVo prizePoolSetVo = new PrizePoolSetVo();
        List<SysDictData> sysDictDataList = this.listByDictType(SysDictEnum.PRIZE_POOL_SET.getCode());
        for(SysDictData sysDictData:sysDictDataList){
            String dictLabel = sysDictData.getDictLabel();
            String dictValue = sysDictData.getDictValue();
            if("oneRewardRote".equals(dictLabel)){
                prizePoolSetVo.setOneRewardRote(new BigDecimal(dictValue));
            }else if("twoRewardRote".equals(dictLabel)){
                prizePoolSetVo.setTwoRewardRote(new BigDecimal(dictValue));
            }else if("threeRewardRote".equals(dictLabel)){
                prizePoolSetVo.setThreeRewardRote(new BigDecimal(dictValue));
            }else if("platformRewardRote".equals(dictLabel)){
                prizePoolSetVo.setPlatformRewardRote(new BigDecimal(dictValue));
            }else if("prizePoolRewardRote".equals(dictLabel)){
                prizePoolSetVo.setPrizePoolRewardRote(new BigDecimal(dictValue));
            }else if("zhituirenshu1".equals(dictLabel)){
                prizePoolSetVo.setZhituirenshu1(Integer.parseInt(dictValue));
            }else if("zhituirenshu2".equals(dictLabel)){
                prizePoolSetVo.setZhituirenshu2(Integer.parseInt(dictValue));
            }else if("zhituirenshu3".equals(dictLabel)){
                prizePoolSetVo.setZhituirenshu3(Integer.parseInt(dictValue));
            }else if("zhituirenshu1PrizeoolRote".equals(dictLabel)){
                prizePoolSetVo.setZhituirenshu1PrizeoolRote(new BigDecimal(dictValue));
            }else if("zhituirenshu2PrizeoolRote".equals(dictLabel)){
                prizePoolSetVo.setZhituirenshu2PrizeoolRote(new BigDecimal(dictValue));
            }else if("zhituirenshu3PrizeoolRote".equals(dictLabel)){
                prizePoolSetVo.setZhituirenshu3PrizeoolRote(new BigDecimal(dictValue));
            }
        }
        return prizePoolSetVo;
    }


    @Override
    public BigDecimal selectPrizePoolBalance() {

        List<SysDictData> sysDictData = this.listByDictType(SysDictEnum.PRIZE_POOL_BALANCE.getCode());
        String dictValue = sysDictData.get(0).getDictValue();
        BigDecimal balance = new BigDecimal(dictValue);
        return balance;
    }

    @Override
    public BigDecimal addPrizePoolBalance(BigDecimal prizePoolBalance) {
        List<SysDictData> sysDictData = this.listByDictType(SysDictEnum.PRIZE_POOL_BALANCE.getCode());
        SysDictData sysDictData1 = sysDictData.get(0);
        String oldBalanceStr = sysDictData1.getDictValue();
        if(StringUtils.isBlank(oldBalanceStr)){
            oldBalanceStr = "0";
        }
        BigDecimal oldBalance = new BigDecimal(oldBalanceStr);
        oldBalance = oldBalance.add(prizePoolBalance);
        sysDictData1.setDictValue(oldBalance.toString());
        boolean b = this.updateById(sysDictData1);
        return oldBalance;
    }


    @Override
    public BigDecimal setPrizePoolBalance(BigDecimal prizePoolBalance) {
        List<SysDictData> sysDictData = this.listByDictType(SysDictEnum.PRIZE_POOL_BALANCE.getCode());
        SysDictData sysDictData1 = sysDictData.get(0);
        sysDictData1.setDictValue(prizePoolBalance.toString());
        boolean b = this.updateById(sysDictData1);
        return prizePoolBalance;
    }

    @Override
    public Map<String, Map<String,String>> multiDictDataMaptByType(String dictType) {
        List<String> split = Arrays.asList(dictType.split(","));
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .in(SysDictData::getDictType, split)
                .eq(SysDictData::getStatus, "1");

        List<SysDictData> sysDictData = sysDictDataDao.selectList(queryWrapper);
        Map<String, List<SysDictData>> collect = sysDictData.stream().collect(Collectors.groupingBy(SysDictData::getDictType));
        Map<String, Map<String,String>> returnMap = new HashMap<>();
        Set<String> mapKeySet = collect.keySet();
        for(String mapKey:mapKeySet){
            List<SysDictData> listItem = collect.get(mapKey);
            Map<String, String> stringSysDictDataMap = listItem.stream().collect(Collectors.toMap(SysDictData::getDictValue, d -> d.getDictLabel(), (o, n) -> n));
            returnMap.put(mapKey,stringSysDictDataMap);
        }
        return returnMap;
    }


}
